/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef WISHMASTER_CHAN_H
#define WISHMASTER_CHAN_H

#include <vector>
#include <QObject>
#include <QPixmap>
#include <QImage>
#include "chanagents/chanagent.h"
#include "taskhub.h"
#include "extracontainer.h"
#include "post.h"
#include "boardconstraints.h"
#include "misc.h"

class QHttp;
class QString;
class QHttpResponseHeader;

namespace Wishmaster
{
	class ThreadStorage;
	class Extractor;
	class Keymap;
	class Board;
	class ChanManager;
	struct PostInfo;

	struct ExtraInfoDescriptor
	{
		QString filename;
		QByteArray info;
	};

	struct AttachmentDescriptor
	{
		AttachmentDescriptor() : useData(false) {}

		bool useData;
		QString filename;
		QByteArray data;
		QList<ExtraInfoDescriptor> extras;
		QString rating;
	};

	struct PostDescriptor
	{
		QString board;
		ThreadId parent_thread;
		QString name;
		QString email;
		QString topic;
		QString text;
		QList<AttachmentDescriptor> attachments;
		QString password;
		bool sage;
	};

	class WISHMASTER_EXPORT Chan : public QObject
	{
		Q_OBJECT
	public:
		Chan(QString const& host, ChanAgent* agent, ChanManager* manager);
		virtual ~Chan();
		ChanAgent* agent() { return m_agent; }
		
		QString const& getHost();
		QString name() const;

		QStringList getBoards();
		QStringList getBoardAliases();
		void addBoard(Board* board);
		Board* board(QString const& name);
		Board* board(int boardNumber);
		int boardsCount() const;

		void setThreadStorage(ThreadStorage* threadstorage);
		ThreadStorage* threadStorage();
		void load();
		void save();
		void swap();

		void setKeymap(Keymap* k);
		Keymap* keymap();

		void setExtractor(Extractor* extractor);
		Extractor* extractor();

		void collectGarbage();
		QByteArray makePostHid(const QString& board, unsigned long threadId, unsigned long postId);

		BoardConstraints constraints(const QString& board = QString::null);
		void setConstraints(const BoardConstraints& constraints);

		bool sageCheckbox();
		void filterThreads(Board* b);
		void filterThread(const ThreadPtr& t);
		void extractWatermarks(ThreadPtr& t);

		QImage icon() const;
		void setIcon(const QImage& i);

	private:
		QString m_host;
		unsigned long m_threadID;
		int m_progress;
		QString m_board;
		ThreadStorage* m_threadStorage;
		Taskhub* m_taskhub;
		ChanAgent* m_agent;
		std::vector<Board*> m_boards;
		Keymap* m_keymap;
		Extractor* m_extractor;
		ChanManager* m_chanmanager;
		BoardConstraints m_constraints;
		QImage m_icon;
	};
}

#endif

